home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / memcpy.c < prev    next >
C/C++ Source or Header  |  1991-11-27  |  4KB  |  143 lines

  1. /*  $Revision: 1.3 $
  2. **
  3. **  This file has been modified to get it to compile more easily
  4. **  on pre-4.4BSD systems.  Rich $alz, June 1991.
  5. */
  6. #define const /* NULL */
  7. #define void char
  8. #define size_t int
  9. #define NULL 0
  10. #define MEMCOPY
  11.  
  12. /*-
  13.  * Copyright (c) 1990 The Regents of the University of California.
  14.  * All rights reserved.
  15.  *
  16.  * This code is derived from software contributed to Berkeley by
  17.  * Chris Torek.
  18.  *
  19.  * Redistribution and use in source and binary forms are permitted provided
  20.  * that: (1) source distributions retain this entire copyright notice and
  21.  * comment, and (2) distributions including binaries display the following
  22.  * acknowledgement:  ``This product includes software developed by the
  23.  * University of California, Berkeley and its contributors'' in the
  24.  * documentation or other materials provided with the distribution and in
  25.  * all advertising materials mentioning features or use of this software.
  26.  * Neither the name of the University nor the names of its contributors may
  27.  * be used to endorse or promote products derived from this software without
  28.  * specific prior written permission.
  29.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  30.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  31.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32.  */
  33.  
  34. #if 0
  35. #if defined(LIBC_SCCS) && !defined(lint)
  36. static char sccsid[] = "@(#)bcopy.c    5.8 (Berkeley) 6/23/90";
  37. #endif /* LIBC_SCCS and not lint */
  38.  
  39. #include <sys/stdc.h>
  40. #include <string.h>
  41. #endif
  42.  
  43. /*
  44.  * sizeof(word) MUST BE A POWER OF TWO
  45.  * SO THAT wmask BELOW IS ALL ONES
  46.  */
  47. typedef    int word;        /* "word" used for optimal copy speed */
  48.  
  49. #define    wsize    sizeof(word)
  50. #define    wmask    (wsize - 1)
  51.  
  52. /*
  53.  * Copy a block of memory, handling overlap.
  54.  * This is the routine that actually implements
  55.  * (the portable versions of) bcopy, memcpy, and memmove.
  56.  */
  57. #ifdef MEMCOPY
  58. void *
  59. memcpy(dst0, src0, length)
  60. #else
  61. #ifdef MEMMOVE
  62. void *
  63. memmove(dst0, src0, length)
  64. #else
  65. void
  66. bcopy(src0, dst0, length)
  67. #endif
  68. #endif
  69.     char *dst0;
  70.     const char *src0;
  71.     register size_t length;
  72. {
  73.     register char *dst = dst0;
  74.     register const char *src = src0;
  75.     register size_t t;
  76.  
  77.     if (length == 0 || dst == src)        /* nothing to do */
  78.         goto retval;
  79.  
  80.     /*
  81.      * Macros: loop-t-times; and loop-t-times, t>0
  82.      */
  83. #define    TLOOP(s) if (t) TLOOP1(s)
  84. #define    TLOOP1(s) do { s; } while (--t)
  85.  
  86.     if ((unsigned long)dst < (unsigned long)src) {
  87.         /*
  88.          * Copy forward.
  89.          */
  90.         t = (int)src;    /* only need low bits */
  91.         if ((t | (int)dst) & wmask) {
  92.             /*
  93.              * Try to align operands.  This cannot be done
  94.              * unless the low bits match.
  95.              */
  96.             if ((t ^ (int)dst) & wmask || length < wsize)
  97.                 t = length;
  98.             else
  99.                 t = wsize - (t & wmask);
  100.             length -= t;
  101.             TLOOP1(*dst++ = *src++);
  102.         }
  103.         /*
  104.          * Copy whole words, then mop up any trailing bytes.
  105.          */
  106.         t = length / wsize;
  107.         TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
  108.         t = length & wmask;
  109.         TLOOP(*dst++ = *src++);
  110.     } else {
  111.         /*
  112.          * Copy backwards.  Otherwise essentially the same.
  113.          * Alignment works as before, except that it takes
  114.          * (t&wmask) bytes to align, not wsize-(t&wmask).
  115.          */
  116.         src += length;
  117.         dst += length;
  118.         t = (int)src;
  119.         if ((t | (int)dst) & wmask) {
  120.             if ((t ^ (int)dst) & wmask || length <= wsize)
  121.                 t = length;
  122.             else
  123.                 t &= wmask;
  124.             length -= t;
  125.             TLOOP1(*--dst = *--src);
  126.         }
  127.         t = length / wsize;
  128.         TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
  129.         t = length & wmask;
  130.         TLOOP(*--dst = *--src);
  131.     }
  132. retval:
  133. #ifdef MEMCOPY
  134.     return(dst0);
  135. #else
  136. #ifdef MEMMOVE
  137.     return(dst0);
  138. #else
  139.     return;
  140. #endif
  141. #endif
  142. }
  143.